/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.domain.WxUserRecommand;
import me.zhengjie.modules.system.service.WxUserRecommandService;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.service.dto.WxUserDto;
import me.zhengjie.modules.system.service.dto.WxUserRecommandDto;
import me.zhengjie.modules.system.service.dto.WxUserRecommandQueryCriteria;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author wangyu
* @date 2021-11-25
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "微信用户推荐管理")
@RequestMapping("/api/wxUserRecommand")
public class WxUserRecommandController {

    private final WxUserRecommandService wxUserRecommandService;

    @Autowired
    private WxUserService wxUserService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('wxUserRecommand:list')")
    public void download(HttpServletResponse response, WxUserRecommandQueryCriteria criteria) throws IOException {
        wxUserRecommandService.download(wxUserRecommandService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询微信用户推荐")
    @ApiOperation("查询微信用户推荐")
    @PreAuthorize("@el.check('wxUserRecommand:list')")
    public ResponseEntity<Object> query(WxUserRecommandQueryCriteria criteria, Pageable pageable){
        Map<String,Object> resultMap = wxUserRecommandService.queryAll(criteria,pageable);
        for (WxUserRecommandDto obj : (List<WxUserRecommandDto>)resultMap.get("content")) {
            WxUser wxUser = wxUserService.findByOpenId(obj.getObjOpenid());
            if (Objects.isNull(wxUser)){
                continue;
            }
            String headimgurl = wxUser.getHeadimgurl();
            if (StringUtils.isBlank(headimgurl)){
                headimgurl = "http://wx.drzh-atmr.cn/static/images/head.jpg";
            }
            obj.setHeadPic(headimgurl);
            obj.setNickName(wxUser.getNickname());
        }
        return new ResponseEntity<>(resultMap,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增微信用户推荐")
    @ApiOperation("新增微信用户推荐")
    @PreAuthorize("@el.check('wxUserRecommand:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody WxUserRecommand resources){
        return new ResponseEntity<>(wxUserRecommandService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改微信用户推荐")
    @ApiOperation("修改微信用户推荐")
    @PreAuthorize("@el.check('wxUserRecommand:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody WxUserRecommand resources){
        wxUserRecommandService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除微信用户推荐")
    @ApiOperation("删除微信用户推荐")
    @PreAuthorize("@el.check('wxUserRecommand:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        wxUserRecommandService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}